/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jmp3player;

import Dane.Mp3File;
import Watki.Graj;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JToolBar;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

/**
 * Klasa reprezentuje odtwarzacz dźwięku w formie paska narzędziowego
 * @author lukeh
 * @version 1.0
 */
public class Odtwarzacz extends JToolBar implements ActionListener, ChangeListener
  {

    String currentFile;
    JButton play, stop, pause, p, m;
    JSlider vol;
    JLabel txt;
    boolean paused, played;
    Graj g;

    /**
     * Konstruktor tworzący wygląd paska
     */
    public Odtwarzacz()
        {
        super();

        play = new JButton("play");
        play.setActionCommand("play");
        play.addActionListener(this);
        this.add(play);
        stop = new JButton("stop");
        stop.setActionCommand("stop");
        stop.addActionListener(this);
        this.add(stop);
        pause = new JButton("pause");
        pause.setActionCommand("pause");
        pause.addActionListener(this);
        this.add(pause);
        txt = new JLabel("");
        JPanel jp = new JPanel();
        jp.setBackground(new Color(0, 0, 0));
        txt.setForeground(new Color(0, 255, 0));
        jp.add(txt);
        vol = new JSlider();
        vol.setMinimum(36);
        vol.setMaximum(100);
        vol.setPaintLabels(false);
        vol.addChangeListener(this);
        this.add(jp);


        this.add(vol);
        }

    /**
     * Ustawia dane pliku który za chwile zostanie odegrany
     * @param addr - adres pliku
     */
    public void setFileToPlay(String addr)
        {
        if (!played)
            {
            currentFile = addr;
            Mp3File m = new Mp3File(addr);
            txt.setText(m.Tytul);
            }
        }

    /**
     * Obsługa zdarzeń przycisków
     * @param arg0
     */
    public void actionPerformed(ActionEvent arg0)
        {
        if (arg0.getActionCommand().compareTo("play") == 0)
            {

            if (played)
                {
                return;
                }
            System.out.println(arg0.getActionCommand());
            g = new Graj(currentFile);
            g.start();
            played = true;
            paused = false;
            }
        if (arg0.getActionCommand().compareTo("stop") == 0)
            {
            g.paused = true;
            g.stopped = true;
            played = false;
            g.stop();
            Mp3File m = new Mp3File(currentFile);
            txt.setText(m.Tytul);
            }
        if (arg0.getActionCommand().compareTo("pause") == 0)
            {
            if (played && paused)
                {
                // g.resume();
                paused = false;
                g.paused = false;
                pause.setText("pause");
                System.out.println("koniec");
                } else if (played && !paused) //try
                {
                //g.wait();
                System.out.println("pauzaaaa");
                paused = true;

                g.paused = true;
                pause.setText("resume");
                }
            }


        }

    /**
     * Odtworzenie załadowanego utworu
     */
    public void Play()
        {
        play.doClick();

        }

    /**
     * Obsługa zdarzeń suwaka głośności
     * @param arg0
     */
    public void stateChanged(ChangeEvent arg0)
        {
        g.volume.setValue(vol.getValue() + g.volume.getMinimum());
        System.out.println(g.volume + " " + vol.getValue());
        }
  }
